import Vue from 'vue'
import VueRouter, { Route, RouteConfig,RawLocation, NavigationGuard ,NavigationGuardNext } from 'vue-router'
import http from "@/utils/request"
import { AxiosResponse } from 'axios'
import Right from '@/domain/Right'

Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  {
    path: '/',
    name: 'first',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import( '../views/Login.vue')
  },
  {
    path: '/modifiy/password',
    name: 'ModifiyPassword',
    component: () => import( '../views/ModifiyPassword.vue')
  },
  {
    path: '/adminCheck',
    name: 'AdminCheck',
    component: () => import( '../views/AdminCheck.vue')
  },
  {
    path: '/resetPassword',
    name: 'ResetPassword',
    component: () => import( '../views/ResetPassword.vue')
  },
  {
    path: '/registration',
    name: 'Registration',
    component: () => import( '../views/Registration.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import( '../views/Home.vue'),
  //   // children:[
  //   //   {
  //   //     path: '/about',
  //   //     name: 'About',
  //   //     component: () => import( '../views/About.vue')
  //   //   }
  //   // ]
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   component: () => import( '../views/About.vue')
  // },
  {
    path: '/*',
    alias:'notFind',
    name: 'NotFind',
    component: () => import('../views/NotFind.vue')
  },
]

const router = new VueRouter({
  routes
})

interface GetRouter{
  (right:Array<Right>):Array<RouteConfig>;
}

let getRouter:GetRouter=(right:Array<Right>):Array<RouteConfig>=> {
  let arr:Array<RouteConfig> =[];
  for (let index = 0; index < right.length; index++) {
    if(right[index].rightType == 1){
      const {rightId,rightName,path,rights} = right[index];
      let children:Array<RouteConfig> = [];
      if(rights && rights.length!=0){
        children= getRouter(rights);
      }

      let rout:RouteConfig = {
        path:path,
        name:rightName,
        children:children
      };
      arr.push(rout);
    }
  }
  return arr;
}


router.beforeEach((to: Route, from: Route,next: NavigationGuardNext<Vue>):any=>{
  if(to.name=='Home'){
    if(localStorage.getItem('token')){
      http({
        url:'/user/info/k',
        method:'get'
      }).then((resp:AxiosResponse<any>):any=>{
        if(resp.data){
          localStorage.setItem('user',JSON.stringify(resp.data));
          let right:Array<Right>=resp.data.items as Array<Right>;
          let childrens:Array<RouteConfig>=getRouter(right);
          for (let index = 0; index < childrens.length; index++) {
            const children:RouteConfig = childrens[index];
            router.addRoute('Home',children);
          }
          next();
        }else{
          next({path:'login'});
        }
      }).catch((res:any):any=>{
        next({path:'login'});
      });
    }else{
      next({path:'login'});
    }
  }else{
    next();
  }
})




export default router
